package us.codecraft.webmagic.core.downloader.okhttp;

import java.net.ProxySelector;

/**
 * @author zhangx <br /> <p>fishlikewater@126.com</p><br />
 * <p> 使用okhttp作为下载客户端</p>
 * @Date 2019年01月24日 12:32
 * @since 0.7.4
 **/
public class OkhttpConfig {

    private static final String JSON_TYPR  = "application/json";
    private static final String FROM_TYPR  = "application/x-www-form-urlencoded";
    private static final String FILR_TYPR  = "application/octet-stream";

    private boolean isOpenAsync = false;

    private long connectionOutTime = 5000;

    private long readOutTime = 5000;

    private long writeTimeout = 5000;

    private boolean retryOnConnectionFailure = false;

    private int maxRequests = 64;

    private int maxRequestsPerHost = 5;

    private int connectionPool = 5;

    private int keepAliveDuration = 5;

    private int retryTimes = 3;

    private boolean autoCookie = false;

    /** ip代理配置**/
    private ProxySelector proxySelector;

    /**
     * 设置ip代理选择器
     * @param proxySelector
     * @return
     */
    public OkhttpConfig setProxySelector(ProxySelector proxySelector){
        this.proxySelector = proxySelector;
        return this;
    }

    public long getConnectionOutTime() {
        return connectionOutTime;
    }

    public OkhttpConfig setConnectionOutTime(long connectionOutTime) {
        this.connectionOutTime = connectionOutTime;
        return this;
    }

    public long getReadOutTime() {
        return readOutTime;
    }

    public OkhttpConfig setReadOutTime(long readOutTime) {
        this.readOutTime = readOutTime;
        return this;
    }

    public long getWriteTimeout() {
        return writeTimeout;
    }

    public OkhttpConfig setWriteTimeout(long writeTimeout) {
        this.writeTimeout = writeTimeout;
        return this;
    }

    public boolean isRetryOnConnectionFailure() {
        return retryOnConnectionFailure;
    }

    public OkhttpConfig setRetryOnConnectionFailure(boolean retryOnConnectionFailure) {
        this.retryOnConnectionFailure = retryOnConnectionFailure;
        return this;
    }

    public int getMaxRequests() {
        return maxRequests;
    }

    public OkhttpConfig setMaxRequests(int maxRequests) {
        this.maxRequests = maxRequests;
        return this;
    }

    public int getMaxRequestsPerHost() {
        return maxRequestsPerHost;
    }

    public OkhttpConfig setMaxRequestsPerHost(int maxRequestsPerHost) {
        this.maxRequestsPerHost = maxRequestsPerHost;
        return this;
    }

    public int getConnectionPool() {
        return connectionPool;
    }

    public OkhttpConfig setConnectionPool(int connectionPool) {
        this.connectionPool = connectionPool;
        return this;
    }

    public int getKeepAliveDuration() {
        return keepAliveDuration;
    }

    public OkhttpConfig setKeepAliveDuration(int keepAliveDuration) {
        this.keepAliveDuration = keepAliveDuration;
        return this;
    }

    public int getRetryTimes() {
        return retryTimes;
    }

    public OkhttpConfig setRetryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
        return this;
    }

    public ProxySelector getProxySelector() {
        return proxySelector;
    }

    public boolean isOpenAsync() {
        return isOpenAsync;
    }

    public OkhttpConfig setOpenAsync(boolean openAsync) {
        isOpenAsync = openAsync;
        return this;
    }

    public boolean isAutoCookie() {
        return autoCookie;
    }

    public OkhttpConfig setAutoCookie(boolean autoCookie) {
        this.autoCookie = autoCookie;
        return this;
    }
}
